﻿using UnityEngine;
using UnityEngine.UI;
namespace UGUIMan
{
    public class Arm:MonoBehaviour
    {
        public enum Side
        {
            Left,Right
        }
        public Side side;
        enum State
        {
            Null,Idle, Spinning,Broke
        }
        private State state;
        Vector3 oriPos;
        public float speed;
        bool Pause;
        public float max = 20;
        public float min = 0.1f;
        void Rot()
        {
            transform.Rotate(new Vector3(0, 0, speed));
        }
        void Smoke()
        {
            transform.Find("Smoke");
        }
        public void speedup()
        {
            if (speed == 0)
            {
                speed = 0.101f;
                Feets.SpeedChange(side.ToString(), speed);
            }
            if (speed < max)
            {
                speed += 2*Time.deltaTime;
            }
            else
            {
                Smoke();
                Void();
            }
        }

        private void Void()
        {
            state = State.Broke;
            speed = 0;
            foreach(Image i in transform.GetComponentsInChildren<Image>())
            {
                i.color -= new Color(0, 0, 0, 0.9f);
            }
        }

        private void Update()
        {
            if (!Pause)
            {
                if (speed > min)
                    speed -= Time.deltaTime;
                else
                {
                    speed = 0;
                    Smoke();
                }
                if (speed > 0)
                    Rot();

                Feets.SpeedChange(side.ToString(), speed);
                if (state == State.Broke)
                {
                    foreach(Image i in this.GetComponentsInChildren<Image>())
                    {
                        i.color +=new Color(0,0,0, Time.deltaTime)/10;
                        if (i.color.a >= 1)
                        {
                            i.color = new Color(i.color.r, i.color.g, i.color.b, 1);
                            state = State.Idle;
                        }
                    }
                }
            }
        }
    }
}